﻿//Author: Jason Leber
//Purpose: Code Behind for Master Page - Login logic
//Date: 11/27/2012
//Modifications: Work Item #, Who, When

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bundled.ORM;
using System.Data.Objects;
using Bundled.Entity;

namespace Bundled
{
    public partial class Bundled : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Member"] != null && Session["ID"] != null)
                {
                    lblWelcome.Text = Session["Member"].ToString();
                    LoggedIn();
                }
                else
                    LoggedOut();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            bool loggedIn = false;
            string username = Utility.SanitizeText(txtLoginName.Text);
            string password = Utility.SanitizeText(txtLoginPW.Text);

            using (BundledDBEntities db = new BundledDBEntities())
            {
                ObjectResult<Get_LoginInfo_Result> result = db.Get_LoginInfo();
                List<Get_LoginInfo_Result> resultList = result.ToList();

                foreach (var resultSet in resultList)
                {
                    if (resultSet.Username == username && resultSet.Password == password)
                    {
                        Session["Member"] = resultSet.Username;
                        Session["ID"] = resultSet.CustomerID;
                        LoggedIn();
                        lblWelcome.Text = resultSet.Username;
                        loggedIn = true;
                    }
                }

                if (!loggedIn)
                {
                    tblLogin.Visible = false;
                    tblIncorrect.Visible = true;
                }
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session["Member"] = null;
            Session["ID"] = null;
            LoggedOut();

            if (Request.Url.AbsolutePath == "/Pages/Account/default.aspx")
                Response.Redirect("/");
        }

        protected void btnRetry_Click(object sender, EventArgs e)
        {
            LoggedOut();
        }

        protected void LoggedIn()
        {
            tblLogin.Visible = false;
            tblLoggedIn.Visible = true;
            tblIncorrect.Visible = false;
            txtLoginName.Text = "";
            txtLoginPW.Text = "";
        }

        protected void LoggedOut()
        {
            tblLogin.Visible = true;
            tblLoggedIn.Visible = false;
            tblIncorrect.Visible = false;
            txtLoginName.Text = "";
            txtLoginPW.Text = "";
        }
    }
}